package com.esun.rabbit2.viewer.mainact.mylotteryact.recharg;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import com.esun.config.Config;
import com.esun.config.Key;
import com.esun.model.UserInfo;
import com.esun.ui.R;
import com.esun.util.log.EDebug;
import com.esun.util.webviewchromeclient.CustomChooseDialog;
import com.esun.util.webviewchromeclient.MyWebViewChromeClient;

/**
 * @ClassName: RechargeYinLianWebView
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author: Kael gaojian@500wan.com
 * @date: 2012-7-24 上午11:10:42
 * 
 */
public class RechargeYinLianWebView extends Activity {
	private ImageButton btnReturn;
	private WebView wvYinLian;
	private Handler handler;
	private String url;
	private Bundle bundle;
	private String TAG;
	private RelativeLayout relativeProgress;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.recharge_yinlian_webview_act);
		init();
		wvYinLian.loadUrl(url);
	}

	private void init() {
		bundle = this.getIntent().getExtras();
		TAG = bundle.getString("TAG");
		handler = new Handler();
		btnReturn = (ImageButton) findViewById(R.id.recharge_yinlian_btn_goBack);
		btnReturn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		relativeProgress = (RelativeLayout)findViewById(R.id.recharge_yinlian_relative_progress);
		wvYinLian = (WebView) findViewById(R.id.webView_rechargeyinlian);
		WebSettings webSettings = wvYinLian.getSettings();
		webSettings.setJavaScriptEnabled(true);
		webSettings.setSaveFormData(false);
		webSettings.setSavePassword(false);
		webSettings.setSupportZoom(false);
		wvYinLian.setWebChromeClient(new MyWebViewChromeClient(relativeProgress));
		wvYinLian.addJavascriptInterface(new JSCallBack(), "client");
		String ck = UserInfo.getUserInfo().getUserSession();
		String username = UserInfo.getUserInfo().getUserName();
		String userpassword = UserInfo.getUserInfo().getUserPwd();
		String platform = Config.NUM1;
		if (TAG.equals(Config.NUM0)) {
			url = bundle.getString(Key.KEY_URL) + "?ck=" + ck + "&username=" + username + "&platform=" + platform;
		} else {
			url = bundle.getString(Key.KEY_URL) + "?ck=" + ck + "&username=" + username + "&password=" + userpassword + "&platform=" + platform;
		}

		EDebug.o("url", "url================" + url);

	}

	class JSCallBack {
		public void showRechargeSuccessDialog(final String title, final String message) {
			EDebug.o("JSCallBack", "JSCallBack被调用");
			handler.post(new ShowDialogRunnable(title, message));

		}
	}

	class ShowDialogRunnable implements Runnable {
		private String mMessage;
		private String mTitle;

		public ShowDialogRunnable(String title, String message) {
			this.mTitle = title;
			this.mMessage = message;
		}

		@Override
		public void run() {
			final CustomChooseDialog dialog = new CustomChooseDialog(RechargeYinLianWebView.this);
			EDebug.o("RechargeBank", mMessage);
			dialog.setTitle(this.mTitle);
			dialog.setMessage(this.mMessage);
			dialog.setLeftButtonText("立即购彩");
			dialog.setOnLeftButtonClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					dialog.dismiss();

					wvYinLian.destroy();

					setResult(RechargeMenuPage.RESULT_CODE_TO_BUY_LOTTERY);

					finish();

				}
			});

			dialog.setRightButtonText("查看账户");
			dialog.setOnRightButtonClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					dialog.dismiss();
					wvYinLian.destroy();
					setResult(RechargeMenuPage.RESULT_CODE_TO_CENTER);

					finish();
				}
			});

			dialog.show();

		}

	}
}
